package homework5;

import java.util.Scanner;

/**
 * Integrate()
 * 
 * @data 2013.7.22
 * @declaration Calculate the integrate
 * @author 121250185
 * 
 * Listing Contents:
 * @method: main(String[] args)
 * 			,pCalculate(double x)
 * 			,fCalculate(double x)
 * 			,gammaCalculate(double x)
 * 			,sumFCalculate(double x, int beginTag)
 */

public class Integrate {

	public static final int NUM_SEG = 10;
	public static final int E = 5;
	public static double W = 0;
	public static int dof = 0;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		System.out.print("Input the x: ");
		double x = Double.valueOf(sc.nextLine());

		System.out.print("Input the dof(degrees of freedom): ");
		dof = Integer.valueOf(sc.nextLine());
		W = x / NUM_SEG;
		System.out.println(String.format(
				("The expected value is: %." + E + "f"), pCalculate(x)));
	}// end

	/**
	 * pCalculate(double x)
	 * 
	 * @declaration: Calculate the p
	 * @param x
	 * @return p
	 */
	public static double pCalculate(double x) {
		double oddCal = 4 * sumFCalculate(x, 1);
		double evenCal = 2 * sumFCalculate(x, 2);
		return (W / 3.0) * (fCalculate(0) + oddCal + evenCal + fCalculate(x));
		
	}// end

	/**
	 * fCalculate(double x)
	 * 
	 * @declaration: Calculate the F
	 * @param x
	 * @return ans
	 */
	public static double fCalculate(double x) {
		double gamma1 = gammaCalculate((dof + 1) / 2.0);
		double gamma2 = gammaCalculate(dof / 2.0);
		return (gamma1 / (Math.sqrt(dof * Math.PI) * gamma2))
				* Math.pow((1 + x * x / dof), -((dof + 1) / 2.0));
		
	}// end

	/**
	 * gammaCalculate(double x)
	 * 
	 * @declaration: Calculate the gamma()
	 * @param x
	 * @return ans
	 */
	public static double gammaCalculate(double x) {
		if (x == 1.0) {
			return 1;
		} else if (x == 0.5) {
			return Math.sqrt(Math.PI);
		} else {
			return (x - 1) * gammaCalculate(x - 1);
		}
		
	}// end

	/**
	 * sumFCalculate(double x, int beginTag)
	 * 
	 * @declaration: Calculate sum of F
	 * @param x
	 *            ,beginTag
	 * @return ans
	 */
	public static double sumFCalculate(double x, int beginTag) {
		double ans = 0;
		for (int i = beginTag; i < NUM_SEG; i += 2) {
			ans += fCalculate(i * W);
		}
		return ans;
		
	}// end

}
